package com.jic.market.common.config.aop;

import com.jic.market.common.util.JsonUtil;
import com.jic.market.common.util.WebUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author guohuayong
 * @date: 2020年01月10日
 */
@Aspect
@Component
public class VisitLogIntercept {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 打印日志与响应时间
     */
    @Around("within(com.jic.market.controller..*Controller)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        long curTime = System.currentTimeMillis();

        ServletRequestAttributes req = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        String method = pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName();

        logger.info("IP：{}，访问：{}，参数：{}", WebUtil.getHost(req.getRequest()), method, JsonUtil.toJson(pjp.getArgs()));

        Object result = pjp.proceed();

        logger.info("方法：{}，返回结果：{}，消耗时间：{}ms", method, JsonUtil.toJson(result), System.currentTimeMillis() - curTime);

        return result;

    }

}
